using UnityEngine;
using System.Collections;

public class DeathCounterControl : StateBehaviour<DeathCounterControl> 
{
    #region Attributes

    /* Counter Speeds */
    public float deathTime = 10.0f;
    public float speedFilling = 1.0f;
    [HideInInspector]
    public float speedEmptying = 1.0f;
	public float speedEmptyingSlow = 0.1f;
    public float speedEmptyingFast = 1.0f;
    public float warningTheshold = 0.25f;

    [HideInInspector]
    public float count = 0.0f;
    [HideInInspector]
    public float cutoffInitial = 0.083333f;
    [HideInInspector]
    public float cutoffDeath = 0.916666f;
    [HideInInspector]
    public float cutoffMin = 0.0f;
    [HideInInspector]
    public float cutoffSlice = 0.166666f;
    [HideInInspector]
    public float cutoffTotal = 0.0f;
    [HideInInspector]
    public float deathTimeRelativeToCutoff = 0.0f;
    [HideInInspector]
    public PlayerControl player;

    private float epsilon = 0.0001f; // Necesario para aproximar los calculos con decimales

    #endregion

    #region Methods

    private void FindPlayer()
    {
        GameObject playerGameObject = GameObject.FindGameObjectWithTag(TagConstants.TAG_PLAYER);
        if (playerGameObject)
            this.player = playerGameObject.GetComponent<PlayerControl>();
        else
            Debug.LogError(TextConstants.PLAYER_NOT_FOUND_ERROR);
    }

    public void ShowDeathCounter()
    {
        this.renderer.enabled = true;
        this.transform.FindChild("LifeBarBackground").renderer.enabled = true;
        this.transform.FindChild("LifeBarOuter").renderer.enabled = true;
    }

    public void HideDeathCounter()
    {
        this.renderer.enabled = false;
        this.transform.FindChild("LifeBarBackground").renderer.enabled = false;
        this.transform.FindChild("LifeBarOuter").renderer.enabled = false;
    }

    public void ChangeEnergy(float quantity, bool permanent)
    {
/*        if(quantity > 0)
            this.State = DeathCounterEmpty.Instance;
        else
            this.State = DeathCounterFill.Instance;
*/
//        this.cutoffTotal += quantity;
//        if (permanent)
//            this.cutoffMin += quantity;        
		float temp = this.deathTime * quantity;
		this.count += temp;
		
		if(this.count < 0)
			this.count = 0;
		
/*		if (this.cutoffMin < this.cutoffInitial)

            this.cutoffMin = this.cutoffInitial;
        else if(this.cutoffMin > this.cutoffDeath)
            this.cutoffMin = this.cutoffDeath;
*/
//        this.renderer.material.SetFloat("_Cutoff", this.cutoffTotal);
    }

    #endregion

    #region Unity Methods

    void Start () 
    {
        this.FindPlayer();
        this.cutoffTotal = this.cutoffInitial;
        this.cutoffMin = this.cutoffInitial;
        this.State = DeathCounterEmpty.Instance;
        this.updateCalledIn = StateBehaviourMethodsName.Update;
		this.speedEmptying = this.speedEmptyingSlow;
    }

    #endregion

    #region StateBehaviour

    public override void OnUpdate()
    {		
        this.deathTimeRelativeToCutoff = this.deathTime * (1 - this.cutoffMin);

        if (this.cutoffTotal + this.epsilon >= this.cutoffDeath)
        {
            this.player.Die();
            this.updateCalledIn = StateBehaviourMethodsName.None;
        }

/*        if (this.State == DeathCounterIdle.Instance && this.player.enemiesInAlert > 0)
        {
            //this.ShowDeathCounter();
			this.speedEmptying = this.speedEmptyingFast;
            this.State = DeathCounterEmpty.Instance;
        }

        if (this.State == DeathCounterIdle.Instance && this.player.enemiesInAlert <= 0)
        {
            //this.HideDeathCounter();
        }
*/
        if (this.State == DeathCounterEmpty.Instance && this.player.enemiesInAlert <= 0)
        {
			this.speedEmptying = this.speedEmptyingSlow;
        }
		
		if (this.State == DeathCounterEmpty.Instance && this.player.enemiesInAlert > 0)
        {
			this.speedEmptying = this.speedEmptyingFast;
        }

        if (this.State == DeathCounterFill.Instance && this.player.enemiesInAlert > 0)
        {
			this.speedEmptying = this.speedEmptyingFast;
            this.State = DeathCounterEmpty.Instance;
        }

        if((1 - this.cutoffTotal) <= this.warningTheshold)
            transform.parent.SendMessage("EnableEffect", true);
        else
            transform.parent.SendMessage("EnableEffect", false);
    }

    public override DeathCounterControl GetBehaviuor()
    {
        return this;
    }

    #endregion
}
